package com.cgnpc.scp.sri.budget.controller;

import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.result.ApiResult;
import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.scp.common.bean.CreateFileInfo;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import com.cgnpc.scp.common.utils.StringUtil;
import com.cgnpc.scp.sri.budget.mybatis.entity.BudgetItem;
import com.cgnpc.scp.sri.budget.service.BudgetInfoService;
import com.cgnpc.scp.sri.budget.service.BudgetItemService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**********************************
 * 用途说明: 调试科研创新管理 - 预算管理 - 预算明细
 * 作者姓名: zhangyibing
 * 创建时间: 2020年10月4日22:01:04
 ***********************************/
@RestController
@RequestMapping("/scp/sri/budget/item")
public class BudgetItemController {
    private static final Logger LOGGER = LoggerFactory.getLogger(BudgetItemController.class);

    @Autowired
    private BudgetItemService budgetItemService;

    @Autowired
    private MessageSource messageSource;

    /**********************************
     * 用途说明: 调试科研创新管理 - 预算明细 - 列表
     * 参数说明 params
     * 返回值说明: 列表
     ***********************************/
    @ApiOperation(value="调试科研创新管理 - 预算明细 - 列表")
//    @UBA(module = "调试科研创新管理", action = "预算明细列表", channel = OperatorType.Page)
    @RequestMapping(value = "/listPage", method = {RequestMethod.POST})
    public AjaxResult listPage(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        try {
            MyPageResult<BudgetItem> result = budgetItemService.queryListPage(params);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,result);
        } catch (Exception e) {
            LOGGER.error("BudgetItemController listPage error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 调试科研创新管理 - 预算明细新增
     * 参数说明 params
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="调试科研创新管理 - 预算明细新增")
    @PostMapping("/saveBudgetItem")
    public AjaxResult saveBudgetItem(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        try {
            return budgetItemService.saveBudgetItem(params);
        } catch (Exception e) {
            LOGGER.error("BudgetItemController saveBudgetItem error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 调试科研创新管理 - 预算明细-导出Excel
     * 参数说明: HttpServletRequest request, @RequestBody T reqEntity
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "调试科研创新管理 - 预算明细 - 导出Excel")
//    @UBA(module = "调试科研创新管理", action = "预算明细 导出", channel = OperatorType.Button)
    @RequestMapping(value = "/exportExcel", method = {RequestMethod.POST})
    @ResponseBody
    public Object exportExcel(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        AjaxResult ajaxResult = null;
        try {
            CreateFileInfo createFileInfo = budgetItemService.exportExcel(params);
            return AjaxResultUtil.createSuccessMessage(createFileInfo);
        } catch (Exception e) {
            LOGGER.error("BudgetItemController exportExcel error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 调试科研创新管理 - 预算明细详情
     * 参数说明 params
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="调试科研创新管理 - 预算明细详情")
    @PostMapping("/info")
    public AjaxResult info(HttpServletRequest request, @RequestBody Map<String, Object> params){
        try {
            String id = params.get("id")+"";
            if("null".equals(id) || StringUtil.isNullOrSpace(id)){
                return AjaxResult.error("id can not be null");
            }
            BudgetItem item = budgetItemService.getItemById(id);
            return AjaxResult.success().put("data", item);
        } catch (Exception e) {
            LOGGER.error("BudgetItemController info error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }

    }

    /**********************************
     * 用途说明: 提交/回退流程
     * 参数说明 params
     * 返回值说明:
     ***********************************/
    @PostMapping("/submitProcess")
    @ApiOperation(value="提交/回退流程")
    public AjaxResult submitProcess(HttpServletRequest request,@RequestBody Map<String, Object> params) {
        try {
            ApiResult apiResult = budgetItemService.submitProcess(params);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,apiResult);
        } catch (Exception e) {
            LOGGER.error("paperController submitProcess error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 预算申请流程删除草稿
     * 参数说明 params
     * 返回值说明:
     ***********************************/
    @PostMapping("/deleteDraft")
    @ApiOperation(value="预算申请流程删除草稿")
    public AjaxResult deleteDraft(HttpServletRequest request,@RequestBody Map<String, Object> params) {
        try {
            ApiResult result = budgetItemService.deleteDraft(params);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,result);
        } catch (Exception e) {
            LOGGER.error("paperController submitProcess error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

}
